package com.dh.jarvis.Util;

import android.text.TextUtils;
import android.util.Log;

import com.dh.jarvis.J;

/**
 * Created by M.c on 2015/12/26.
 */
public class LogUtil {


    private static String generateTag() {
        StackTraceElement caller = new Throwable().getStackTrace()[2];
        String tag = "%s.%s(L:%d)";
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
        tag = TextUtils.isEmpty(J.FRAMEWORK_NAME) ? tag : J.FRAMEWORK_NAME + ":" + tag;
        return tag;
    }

    public static void d(String content) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.d(tag, content);
    }

    public static void d(String content, Throwable tr) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.d(tag, content, tr);
    }

    public static void e(String content) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.e(tag, content);
    }

    public static void e(String content, Throwable tr) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.e(tag, content, tr);
    }

    public static void i(String content) {
        if (!J.getDebug()) return;
        String tag = generateTag();
        int index = 0;
        int maxLength = 4000;
        String str ;
        while (index < content.length()) {

            if (content.length() <= index + maxLength) {
                str = content.substring(index);
            } else {
                str = content.substring(index, index+maxLength);
            }
            index += maxLength;
            Log.i(tag, str);
        }
    }

    public static void i(String content, Throwable tr) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.i(tag, content, tr);
    }

    public static void v(String content) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.v(tag, content);
    }

    public static void v(String content, Throwable tr) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.v(tag, content, tr);
    }

    public static void w(String content) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.w(tag, content);
    }

    public static void w(String content, Throwable tr) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.w(tag, content, tr);
    }

    public static void w(Throwable tr) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.w(tag, tr);
    }


    public static void wtf(String content) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.wtf(tag, content);
    }

    public static void wtf(String content, Throwable tr) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.wtf(tag, content, tr);
    }

    public static void wtf(Throwable tr) {
        if (!J.getDebug()) return;
        String tag = generateTag();

        Log.wtf(tag, tr);
    }


}
